package com.cl.erp_2301.controller;

import com.cl.erp_2301.dto.DataDTO;
import com.cl.erp_2301.dto.GoodsTypeDTO;
import com.cl.erp_2301.entity.Emp;
import com.cl.erp_2301.entity.GoodsType;
import com.cl.erp_2301.mapper.GoodsTypeMapper;
import com.cl.erp_2301.Service.GoodsTypeService;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v2")
public class GoodsTypeController {
    @Resource
    private GoodsTypeService goodsTypeService;

//    @ResponseBody
//    @RequestMapping("/goodstype/listByPage")
//    public Result<List<GoodsType>> listByPage(Integer page, Integer rows) {
//        Page<Object> pages = PageHelper.startPage(page, rows);
//        List<GoodsType> goodsTypeList = goodsTypeService.listByPage();
//        PageInfo<Object> pageInfo = new PageInfo<>(goodsTypeList);
//        return Result.ok(goodsTypeList);
//    }

    @GetMapping("/goodstype/listByPage")
    public Result listByPage(int page, int rows) {
        Page<Object> marketPage = PageHelper.startPage(page, rows);
        DataDTO data = new DataDTO();
        data.setRows(goodsTypeService.listByPage());
        data.setTotal(marketPage.getTotal());
        return Result.ok(data);
    }

    //模糊查询
    @ResponseBody
    @RequestMapping("/formSearch")
    public Result formSearch(String name) {
        DataDTO dataDTO = new DataDTO<>();
        dataDTO.setRows(goodsTypeService.findByNameLike(name));
        return Result.ok(dataDTO);
    }

    @ResponseBody
    @RequestMapping("/goodstype/selectone")
    public Result selectone(@RequestParam("id") Integer id,HttpServletRequest request) {
        DataDTO dataDTO = new DataDTO<>();
        request.getSession().setAttribute("updateId",id);
        dataDTO.setRows(goodsTypeService.selectone(id));
        return Result.ok();
    }

    @RequestMapping( "//goodstype/update")
    public Result update(@RequestParam("name") String name, GoodsTypeDTO goodsTypeDTO,HttpSession session) {
        Integer uuid = (Integer) session.getAttribute("updateId");
        goodsTypeDTO.setName(name);
        goodsTypeDTO.setUuid(uuid);
        goodsTypeService.update(goodsTypeDTO);
        return Result.ok();
    }


    @ResponseBody
    @PostMapping(value = "//goodstype/add")
    public Result add(@RequestParam("name") String name) {
        GoodsTypeDTO goodsTypeDTO=new GoodsTypeDTO();
        goodsTypeDTO.setName(name);
        goodsTypeService.add(goodsTypeDTO);
        return Result.ok();
    }
    @RequestMapping(value = "/goodstype",method = RequestMethod.DELETE)
    public Result deleteById(@RequestParam(value = "id",required = false,defaultValue = "")Integer id){
        goodsTypeService.deleteById(id);
        return Result.ok();
    }

}
